﻿using CountDown.Properties;
using System;
using System.Drawing;
using System.Media;
using System.Windows.Forms;

namespace CountDown
{
    public partial class Main : Form
    {
        public static Form MainForm { get; set; }
        private static TimeSpan ts;
        private static int Second = 0; //设置的秒
        private static Timer timer;
        private static Label label;
        private SetForm setForm = new SetForm();

        public Main()
        {
            MainForm = this;
            InitializeComponent();
            Init();
        }


        private void Init()
        {
            //窗体
            this.StartPosition = FormStartPosition.Manual; //窗体的位置由Location属性决定
            this.TopMost = true;
            this.Location = new Point(0, 0);
            this.FormBorderStyle = FormBorderStyle.None;//不显示标题栏和任务栏
            this.ShowInTaskbar = false;
            this.Width = 255;
            this.Height = 56;
            this.BackColor = Color.White;
            this.Text = "维尔纳斯-会议倒计时";

            //定时器
            timer = new Timer
            {
                Interval = 1000
            };
            timer.Tick += Timer_Tick;
            timer.Start();

            //显示
            label = new Label
            {
                AutoSize = true,
                Location = new Point(55, 0),
                ForeColor = Color.Red,
                Font = new Font("隶书", 36, FontStyle.Bold),
                BackColor = Color.Yellow,
                Cursor = Cursors.Help
            };
            label.DoubleClick += Label_DoubleClick;
            this.Controls.Add(label);

            //开始暂停
            PictureBox pbSt = new PictureBox
            {
                Image = Resources.st,
                Location = new Point(5, 5),
                Cursor = Cursors.Hand
            };
            pbSt.Click += PbSt_Click;
            this.Controls.Add(pbSt);

            //恢复
            PictureBox pbR = new PictureBox
            {
                Image = Resources.r,
                Location = new Point(205, 5),
                Cursor = Cursors.Hand
            };
            pbR.Click += PbR_Click;
            this.Controls.Add(pbR);
        }



        private void PbSt_Click(object sender, EventArgs e)
        {
            if (timer.Enabled)
                timer.Stop();
            else
                timer.Start();
        }

        private void PbR_Click(object sender, EventArgs e)
        {
            Reset();
        }

        private void Label_DoubleClick(object sender, EventArgs e)
        {
            setForm.Show();
        }


        private void Timer_Tick(object sender, EventArgs e)
        {
            if (ts.TotalSeconds > 0)
            {
                ts = ts.Subtract(new TimeSpan(0, 0, 1));
            }
            ViewTime();
            switch (ts.TotalSeconds)
            {
                case 0:
                    Reset();
                    break;
                case 7:
                    PlayWav();
                    break;
                default:
                    if (ts.TotalSeconds < 0)
                    {
                        timer.Stop();
                    }
                    break;
            }

        }

        private void PlayWav()
        {
            new SoundPlayer(Resources.nz).Play();
        }

        private void Reset()
        {
            if (Second > 0)
            {
                SetInitialTime(Second);
            }
        }

        private static void ViewTime()
        {
            label.Text = ts.Minutes.ToString().PadLeft(2, '0') + ":" + ts.Seconds.ToString().PadLeft(2, '0');
        }

        public static void SetInitialTime(int second)
        {
            Second = second;
            ts = new TimeSpan(0, 0, second);
            ViewTime();
            if (!timer.Enabled)
                timer.Start();
        }

        public static void SetLocation(String buttonName)
        {
            switch (buttonName)
            {
                case "LeftTop":
                    MainForm.Location = new Point(0, 0);
                    break;
                case "LeftBottom":
                    MainForm.Location = new Point(0, Screen.PrimaryScreen.Bounds.Height - MainForm.Height);
                    break;
                case "Center":
                    MainForm.Location = new Point(
                        (Screen.PrimaryScreen.Bounds.Width - MainForm.Width) / 2,
                        (Screen.PrimaryScreen.Bounds.Height - MainForm.Height) / 2);
                    break;
                case "RightTop":
                    MainForm.Location = new Point(Screen.PrimaryScreen.Bounds.Width - MainForm.Width, 0);
                    break;
                case "RightBottom":
                    MainForm.Location = new Point(Screen.PrimaryScreen.Bounds.Width - MainForm.Width, Screen.PrimaryScreen.Bounds.Height - MainForm.Height);
                    break;
            }
        }
        public static void SetOpacity(double val)
        {
            MainForm.Opacity = val;
        }
    }
}
